<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script>
			"use strict"
			
			// 模拟指针对象(遍历器对象)
			function myIterator(){  // iterator接口
				let nextIndex = 0; // 记录指针的位置
				return {  // 遍历器对象
					next: function(){
						return nextIndex < arr.length ? {value: arr[nextIndex++], done: false} : {value: arr[nextIndex++], done: true}
					}
				}
			}
			
			// 准备一个数据
			let arr = [1, 4, 65, "abc"]
			let iteratorObj = myIterator(arr)
			console.log(iteratorObj.next())
			console.log(iteratorObj.next())
			console.log(iteratorObj.next())
			console.log(iteratorObj.next())
			console.log(iteratorObj.next())
			console.log(iteratorObj.next())
			
			
			console.log("\n")
			for(let i of arr){
				console.log(i)
			}
			console.log("\n")
			for(let i of arr[3]){
				console.log(i)
			}
//			console.log("\n")
//			var obj = {username: "kobe", age: 21}
//			for(let i of obj){
//				console.log(i)
//			}



			// 等同于在指定的数据结构上部署了iterator接口
			// 当使用for of 去遍历某一个数据结构的时候, 首先去找Symbol.iterator, 找到了就去遍历, 没找到的话不能遍历: xxx is not iterable
			let targetData = {
				[Symbol.iterator]: function(){
					let nextIndex = 0; // 记录指针的位置
					return {  // 遍历器对象
						next: function(){
							return nextIndex < this.length ? {value: this[nextIndex++], done: false} : {value: this[nextIndex++], done: true}
						}
					}
				}
			}
			
			// 使用三点运算符, 解构赋值, 默认去调用iterator接口
			let arr2 = [1, 6]
			let arr3 = [2, 3, 4]
			arr2 = [1, ...arr3, 6]
			console.log(arr2)
			
			let [a, b] = arr2
			console.log(a, b)
		</script>
	</body>
</html>
